#include<iostream>
#include<errno.h>
#include<string.h>
#include"Socket.hpp"


int main()
{
    TcpSocket ser;
    ser.Build_Server_Socket(8888,"127.0.0.1");
    // NonBlock(ser.Fd());
    while(true){
        struct sockaddr_in client_addr;
        int addrlen = sizeof(client_addr);
        int connfd = ser.Accept((struct sockaddr*)&client_addr,&addrlen);
        if(connfd < 0){
            LOG_ERR("accept error:%s",strerror(errno));
            continue;
        }
        TcpSocket clientfd(connfd);//客户端连接(一个新连接就建立一个TcpSocket对象)
        char buf[1024];
        int n = clientfd.Recv(buf,sizeof(buf)-1);
        if(n<=0){
            LOG_ERR("recv error:%s",strerror(errno));
            continue;
        }
        buf[n] = '\0';
        std::string sendmsg = "server send ";
        sendmsg += buf;
        int m = clientfd.Send(sendmsg.c_str(),sendmsg.size());
        if(m<=0){
            LOG_ERR("send error:%s",strerror(errno));
            continue;
        }
        std::cout<<"wait..."<<std::endl;
        sleep(1);
    }
    return 0;
}


